package com.shujia.MR;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class MapReduce03_03 {
    public static class MyMapper extends Mapper<LongWritable, Text, Text, Text> {
        @Override
        protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, Text>.Context context) throws IOException, InterruptedException {
            String xmlb = value.toString().split("\t")[0].split(",")[0];
            String id = value.toString().split("\t")[0].split(",")[1];
            double sum_dkje = Double.parseDouble(value.toString().split("\t")[1]);


            context.write(new Text(xmlb), new Text(id + "," + sum_dkje));
        }
    }

    public static class MyReducer extends Reducer<Text, Text, Text, NullWritable> {
        @Override
        protected void reduce(Text key, Iterable<Text> values, Reducer<Text, Text, Text, NullWritable>.Context context) throws IOException, InterruptedException {
            ArrayList<String> arr = new ArrayList<>();
            for (Text value : values) {
                arr.add(value.toString());
            }

            // 进行排序
            Collections.sort(arr, new Comparator<String>() {
                @Override
                public int compare(String o1, String o2) {
                    Double this_sum_dkje = Double.parseDouble(o1.split(",")[1]);
                    Double that_sum_dkje = Double.parseDouble(o2.split(",")[1]);

                    return that_sum_dkje.compareTo(this_sum_dkje);
                }
            });

            // 去Top 3
            for (int i = 0; i < arr.size() && i < 3; i++) {
                context.write(new Text(key.toString() + "," + arr.get(i)), NullWritable.get());
            }
        }
    }

    // Driver
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        Configuration conf = new Configuration();

        Job job = Job.getInstance(conf);
        job.setJobName("MapReduce03_03");
        job.setJarByClass(MapReduce03_03.class);

        // 设置Map端
        job.setMapperClass(MyMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);
        // 设置Reduce端
        job.setReducerClass(MyReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(NullWritable.class);

        // 设置输入输出路径
        FileInputFormat.addInputPath(job, new Path("/data/MR/output3-1/"));

        // 判断输出路径是否存在
        FileSystem fs = FileSystem.get(conf);
        Path path = new Path("/data/MR/output3-3");
        if (fs.exists(path)) {
            fs.delete(path, true);
        }

        FileOutputFormat.setOutputPath(job, path);

        job.waitForCompletion(true);

        // hadoop jar Competition2021-1.0.jar com.shujia.MR.MapReduce03
    }
}
